<?php echo $header; ?>
<div class="alert-wrap">
    <div class="alert-create-k alert-position">
    </div>
</div>
<div class="container in-wrap">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <h1 class="text-center"><?php echo $maincontent; ?></h1>
        </div>
    </div>
    <form class="form-horizontal" method="post" action="" id="c-form">
        <div class="form-group from-margin-top">
            <label for="" class="control-label col-md-2 col-md-offset-1">知识点名称</label>
            <div class="col-md-6">
                <input type="text" name="knowledgename" id="knowledgename" class="form-control" placeholder="最好是能说明内容的名称">
            </div>
        </div>
        <div class="form-group from-margin-top">
            <label for="" class="control-label col-md-2 col-md-offset-1">知识点标签</label>
            <div class="col-md-6">
                <input type="text" name="knowledgelabel" id="knowledgelabel" class="form-control" placeholder="查询时有用，用英文逗号分隔" value="<?php echo $defaultlabel; ?>">
            </div>
        </div>
        <div class="form-group has-feedback from-margin-top option-container">
            <label for="" class="control-label col-md-2 col-md-offset-1">初次完成时间</label>
            <div class="col-md-6 date datetp create-k-datetimepicker">
                <input type="text" name="firstfinishtime" id="firstfinishtime" class="form-control" placeholder="YYYY-MM-DD HH:MM:SS" readonly>
                <div class="pull-right datetp-fix">
                    <span class="glyphicon glyphicon-calendar form-control-feedback create-canlendar"></span>
                </div>
            </div>
        </div>
        <div class="form-group from-margin-top">
            <label for="" class="control-label col-md-2 col-md-offset-1">知识点详情</label>
            <div class="col-md-6">
                <textarea name="knowledgedetail" id="knowledgedetail"></textarea>
            </div>
        </div>
        <div class="form-group from-margin-top">
            <div class="col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
            <a href="javascript:void(0);" class="btn btn-primary create-btn" id="auto-fill">
                        自动填充
                    </a>
                <a href="javascript:void(0);" class="btn btn-primary create-btn" id="submit-create-k">
                        提交
                    </a>
                <button type="reset" class="btn btn-primary create-btn">重置</button>
            </div>
        </div>
    </form>
</div>
<?php echo $footerin;?>
<script>
$(function() {
    var e;
    var $target = $(".alert-create-k");
    var velnameurl = "<?php echo $velknameurl; ?>";
    $("#firstfinishtime").val(getNowFormatDate());
    //时间选择插件
    $(".create-k-datetimepicker").datetimepicker({
        container: $(".option-container")
    });
    //名称验证
    $("#knowledgename").on("blur", function() {
        var kname = $(this).val();
        if (e = kname_vel(kname, 0, velnameurl, $target)) {
            alert2000($target, e);
            return false;
        };
    });
    //标签验证
    $("#knowledgelabel").on("blur", function() {
        var klabel = $(this).val();
        if (e = klabel_vel(klabel)) {
            alert2000($target, e);
            return false;
        }
    });
    //时间验证
    $("#firstfinishtime").on("blur", function() {
        var ftime = $(this).val();
        if (e = ktime_vel(ftime)) {
            alert2000($target, e);
            return false;
        }
    });
    //详情验证
    $("#knowledgedetail").on("blur", function() {
        var kdetail = $(this).val();
        if (e = kdetail_vel(kdetail)) {
            alert2000($target, e);
            return false;
        }
    });
    //自动填充
    $("#auto-fill").on("click",function(){
        var kdetail = $("#knowledgedetail").val();
        var knameandkdetail = kdetail_autoget(kdetail);
        //alert(JSON.stringify(knameandkdetail));
        $("#knowledgename").val(knameandkdetail.kname);
        $("#knowledgedetail").val(knameandkdetail.kdetail);
        $("#firstfinishtime").val(getNowFormatDate());
        var autoklabel = knameandkdetail.klabel?(knameandkdetail.klabel+','):'';
        $("#knowledgelabel").val(autoklabel+$("#knowledgelabel").val());
    })
    //点击提交验证
    $("#submit-create-k").on("click", function() {

        var kname = $("#knowledgename").val();
        var klabel = $("#knowledgelabel").val();
        var ftime = $("#firstfinishtime").val();
        var kdetail = $("#knowledgedetail").val();
        //autofill
        var idinfo;
        try {
            if (idinfo = kname_vel(kname, 0, velnameurl, $target)) {
                throw idinfo;
            };
            if (idinfo = klabel_vel(klabel)) {
                throw idinfo;
            };
            if (idinfo = ktime_vel(ftime)) {
                throw idinfo;
            };
            if (idinfo = kdetail_vel(kdetail)) {
                throw idinfo;
            };

        } catch (e) {
            alert2000($target, e);
            return false;
        }
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "",
            data: { kname: kname, klabel: klabel, ftime: ftime, kdetail: kdetail },
            beforeSend: function() {
                idinfo = backalert + "正在添加数据请稍后……</div>";
                $target.html(idinfo).fadeIn();
            },
            success: function(data) {
                if (data) {
                    idinfo = "成功添加了第" + data + "条数据";
                    alert2000($target, idinfo);
                    $("#c-form")[0].reset();
                    $("#firstfinishtime").val(getNowFormatDate());
                } else {
                    idinfo = "数据插入失败！请重试";
                    alert2000($target, idinfo);

                }
            }
        })
    })
})
</script>
<?php echo $footer;?>